<?php
    $materialID = $_GET['materialID'];
    $libraryCardNo = $_GET['libraryCardNo'];
    $borrow_date = date("Y-m-d");
    $libraryNo = $_COOKIE['libraryNo'];
        borrow_material($materialID,$borrow_date);

  function borrow_material ($materialID,$borrow_date)
  {
    require('db_connection.php');
    //check for book details
    $libraryNo = $_COOKIE['libraryNo'];
    $materialID = $_GET['materialID'];
    $libraryCardNo = $_GET['libraryCardNo'];
    
    $query2 = "SELECT * FROM tblcollection WHERE materialID like '$materialID' AND libraryNo='$libraryNo'";
    $result2= mysql_query($query2);
    while ($row = mysql_fetch_array($result2))
    {
      $title = $row['title'];
      $author = $row['author'];
      $libraryNo = $row['libraryNo'];
      $copyNo = $row['copyNo']-1;
    }
    
    $query6 = "SELECT * FROM tblclient WHERE libraryCardNo like '$libraryCardNo' AND libraryNo='$libraryNo'";
    $result6 = mysql_query($query6);
        while($row=mysql_fetch_array($result6))
        {
          $userID = $row['userID'];
        }
    //$query = "UPDATE tblcollection SET  status='Borrowed' WHERE materialID like '$materialID'";
    $query = "INSERT INTO tblcirculation VALUES('$userID', 'borrowed','',$libraryNo,'$borrow_date','','','', $materialID)";
    $result = mysql_query($query);
    //$query3 = "UPDATE tblcirculation SET dateReserved='$reserve_date' WHERE materialID like '$materialID'";
    //$userID = $_COOKIE['user'];
    //$query3 = "INSERT INTO tblcirculation VALUES($userID, 'reserved','',$libraryNo,'','','$reserve_date','', $materialID)";
    //$result3 = mysql_query($query3);
    //echo "$query3";
    //$query2 = "SELECT * FROM tblcollection WHERE materialID like '$materialID'";
    //$result2= mysql_query($query2);

    //if($result && $result3)
    //{
        echo"<script> alert('Borrowing successful.');</script>";
       // echo"<a href='index.php'> Continue</a>";

        //echo("<script> document.location.href='circulation.php?libraryCardNo=$libraryCardNo'</script>");
    //}
    //else
    //echo"<script> alert('Reservation failed.');</script>";



  }
  //echo $libraryCardNo;
 echo("<script> document.location.href='transactions.php?libraryCardNo=$libraryCardNo&libraryNo=$libraryNo'</script>");

?>
